package c.a.md.androidvideo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.io.IOException;

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback, TextureView.SurfaceTextureListener
{

    private TextureView textureView;
    private SurfaceView surface;
    private Switch aSwitch;

    private Camera mCamera;

    private SurfaceHolder mHolder;
    private SurfaceTexture mTexture;
    private boolean isTexturePreview;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        surface = findViewById(R.id.surface);
        textureView = findViewById(R.id.texure);
        aSwitch = findViewById(R.id.change);

        surface.getHolder().addCallback(this);
        textureView.setSurfaceTextureListener(this);

        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                isTexturePreview = isChecked;

                aSwitch.setText(isChecked ? "Texture预览" : "Surface预览");

                changePreviewSurface();
            }
        });
    }

    private void changePreviewSurface()
    {
        destroyCamera();
        initCamera(isTexturePreview);
    }   

    private void initCamera(boolean isTexture)
    {
        // 打开摄像头并将展示方向旋转90度
        mCamera = Camera.open();
        mCamera.setDisplayOrientation(90);
        try
        {
            if(!isTexture)
            {
                mCamera.setPreviewDisplay(mHolder);
            }else
            {
                mCamera.setPreviewTexture(mTexture);
            }
            mCamera.startPreview();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void destroyCamera()
    {
        try
        {
            if (mCamera != null)
            {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        } catch (Exception ee)
        {

        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        mHolder = holder;

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 9);
        } else
        {
            initCamera(isTexturePreview);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        if (mCamera != null)
        {
            mCamera.stopPreview();
            mCamera = null;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 9)
        {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                initCamera(isTexturePreview);
            }
        }
    }

    @Override
    protected void onDestroy()
    {
        destroyCamera();
        super.onDestroy();
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
    {
        mTexture = surface;
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 9);
        } else
        {
            initCamera(isTexturePreview);
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
    {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
    {
        destroyCamera();
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface)
    {

    }
}
